#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "include/structs.h"
#include "include/fncts.h"
#include "include/events.h"
#include "include/menu.h"
char *getpass(const char *prompt);
char *get_current_dir_name(void);
int main()
{
	int n,out=0,idrech,test=0,medout=0,patout=0;
	char mdp[20],usr[20],nom[20];
	staffmed med1;
	patient pat1;
	out = Auth_test(usr,mdp);
	while(out==0)
        {
        afficher_menu();
        switch(choix_menu())
        {
          case 1:{	medout=0;
			while(medout==0)
			{
				afficher_menu_med();
				switch(choix_menu())
				{
					case 1:{
                                                printf("\033[2J");
						saisie_Med(&med1);add_med(med1,"/tmp/dbmed");break;
					       }
					case 2:{
						printf("\033[2J");
                        			ListMed("/tmp/dbmed");
                        			break;
					       }
					case 3:{
							modifier_med("/tmp/dbmed");break;
						}
					case 4:{
						printf("\033[2J");
                        			printf("Nom du staff medical rechercher :");
                        			scanf("%s",nom);
						test=0;
						idrech=medtoid("/tmp/dbmed",nom);
                        			if(idrech!=-1){test=read_med_file("/tmp/dbmed",idrech,&med1);}
                        			if(test==1)
                       				{ printf("ID: %d | Nom: %s | Prenom: %s | CIN: %ld \n",med1.id,med1.nom,med1.prenom,med1.cin); }
                        			else{printf("%s introuvable \n",nom);}
                        			break;
						}
					case 5:{
						printf("\033[2J");
                        			printf("ID du med a supprimer :");
                        			scanf("%d",&idrech);
                        			supprimer_med("/tmp/dbmed",idrech);
                        			break;
					       }
					case 6:medout=1;break;
					default:printf("Mauvais choix\n");

				}
			}
			break;
		 }
          case 2:{	patout=0;
			while(patout==0)
			{
				afficher_menu_pat();
				switch(choix_menu())
				{
					case 1:{
						printf("\033[2J");
						saisie_Pat(&pat1);add_patient(pat1,"/tmp/dbpat");break;
						}
					case 2:{
						printf("\033[2J");
                        			ListPat("/tmp/dbpat");
                        			break;
					       }
					case 3:{
							modifier_pat("/tmp/dbpat");break;
						}
					case 4:{
						printf("\033[2J");
						printf("Nom du patient rechercher : ");
                        			scanf("%s",nom);
                                                test=0;
                                                idrech=pattoid("/tmp/dbpat",nom);
                                                if(idrech!=-1){test=read_pat_file("/tmp/dbpat",idrech,&pat1);}
                                                if(test==1)
						{ printf("ID: %d | Date d'admission : %s |Nom: %s | Prenom: %s | Maladie: %s | CIN: %ld \n",pat1.id,pat1.date_admission,pat1.nom,pat1.prenom,pat1.Nom_maladie,pat1.cin); }
                        			else{printf("%d introuvable \n",idrech);}
                        			break;
						}
					case 5:{
						printf("\033[2J");
                        			printf("ID du pat a supprimer :");
                        			scanf("%d",&idrech);
                        			supprimer_pat("/tmp/dbpat",idrech);
                        			break;
					       }
					case 6:patout=1;break;
					default:printf("Mauvais choix\n");

				}
			}
			break;
		 }
	  case 3:{
			n=n_Lines("/tmp/events.log");
			if(n!=-1)
			{
			printf("\033[2J");
			printf("----DEBUT-ANALYSE---- \n");
			ReadEvent("/tmp/events.log",n);
			printf("----FIN---ANALYSE---- \n");
			}
			break;
		 }
          case 99:out=1;break;
          default:printf("Mauvais choix\n");
        }
        }
	//n=n_Lines("/tmp/events.log");
	//if(n!=-1)ReadEvent("/tmp/events.log",n);
return 0;
}
